package io.github.lee.jel.ui.main.home

import android.app.Application
import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.lee.core.vm.BaseViewModel
import io.github.lee.core.vm.err.NoMoreDataException
import io.github.lee.jel.R
import io.github.lee.repository.jel.data.session
import io.github.lee.repository.jel.domain.Item
import io.github.lee.repository.jel.repo.ItemRepository
import io.github.lee.repository.jel.repo.UserRepository
import javax.inject.Inject

@HiltViewModel
class HomeVM @Inject constructor(
    application: Application,
    private val repo: UserRepository,
    private val itemRepo: ItemRepository,
) : BaseViewModel(application) {
    val homeDataLiveData = MutableLiveData<List<Pair<Item, List<Item>?>>>()


    fun getHomeData() {
        val session = mContext.session
        if (null == session) {
            empty()
            return
        }
        launchResult(
            block = {
                val homeData = mutableListOf<Pair<Item, List<Item>?>>()
                val playingList = itemRepo.localPlaying().items
                if (!playingList.isNullOrEmpty()) {

                    homeData.add(Item(name = getString(R.string.txt_playing)) to playingList)
                }
                homeData
            },
            error = {
                if (homeDataLiveData.value.isNullOrEmpty()) {
                    error(it)
                } else {
                    toast(it?.format(mContext))
                }
                hideProgress()
            }
        ) {
            if (it.isEmpty()) {
                empty(NoMoreDataException(mContext))
            } else {
                homeDataLiveData.postValue(it)
                success()
            }
            hideProgress()
        }
        fetchRemote(true)
    }

    fun fetchRemote(showProgress: Boolean = false) {
        launchResult(
            block = {
                val homeData = mutableListOf<Pair<Item, List<Item>?>>()

                // 最近播放
                if (showProgress) showProgress(getString(R.string.txt_start_syncing_playing), false)
                val playingResult = itemRepo.playingList(mContext.session?.userId)
                val playingList = playingResult.data?.items
                if (!playingList.isNullOrEmpty()) {
                    homeData.add(
                        Item(name = getString(R.string.txt_playing))
                                to playingList
                    )
                }

                if (showProgress) showProgress(getString(R.string.txt_start_syncing_folder), false)
                val userViewsResult = repo.views(mContext.session?.userId)
                val userViews = userViewsResult.data?.items
                userViews?.forEach {
                    // 同步最新入库
                    if (showProgress) showProgress(
                        getString(R.string.txt_start_syncing, it.name),
                        false
                    )
                    val name = it.name
                    if (!name.isNullOrEmpty()) {
                        val latestResult = itemRepo.latest(mContext.session?.userId, it.id)
                        val latest = latestResult.data
                        if (!latest.isNullOrEmpty()) {
                            homeData.add(it to latest)
                        }
                    }
                }
                homeData
            },
            error = {
                if (homeDataLiveData.value.isNullOrEmpty()) {
                    error(it)
                } else {
                    toast(it?.format(mContext))
                }
                refreshSuccess()
                hideProgress()
            }
        ) {
            refreshSuccess()
            if (it.isEmpty()) {
                if (homeDataLiveData.value.isNullOrEmpty()) {
                    empty(NoMoreDataException(mContext))
                } else {
                    toast(NoMoreDataException(mContext).text)
                }
            } else {
                homeDataLiveData.postValue(it)
                success()
            }
            hideProgress()
        }
    }

}